package com.zdkj.subway.st.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.exceltools.ExcelUtil;
import com.zdkj.common.core.exceltools.IExcelExport;
import com.zdkj.common.core.exceltools.IExcelParser;
import com.zdkj.common.core.util.R;
import com.zdkj.common.core.util.SpringContextHolder;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.st.entity.SwStCycle;
import com.zdkj.subway.st.service.SwStCycleService;
import com.zdkj.subway.st.util.MultipartFileToFile;
import com.zdkj.subway.st.util.SubwayStConstants;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/swStCycle")
@Api(value = "swStCycle", tags = "站间调票")
public class SwStCycleController {
    private final SwStCycleService swStCycleService;

    @GetMapping("/{id}")
    public R getById(@PathVariable String id) {
        return R.ok(swStCycleService.getById(id));
    }

    @GetMapping("/page")
    public R<IPage> getDictPage(Page page, SwStCycle swStCycle) {
        List<String> dateConditiondr = swStCycle.getDates1();
        List<String> dateConditiondc = swStCycle.getDates2();
        QueryWrapper queryWrapper = Wrappers.query(swStCycle);
        if (ObjectUtil.isNotEmpty(dateConditiondr) && !"".equals(dateConditiondr.get(0))) {
            queryWrapper.between("date_format(in_time,'%Y-%m-%d')", dateConditiondr.get(0), dateConditiondr.get(1));
        }
        if (ObjectUtil.isNotEmpty(dateConditiondc) && !"".equals(dateConditiondc.get(0))) {
            queryWrapper.between("date_format(out_time,'%Y-%m-%d')", dateConditiondc.get(0), dateConditiondc.get(1));
        }
        return R.ok(swStCycleService.page(page, queryWrapper));
    }

    @SysLog("申请站间调票")
    @PostMapping
    public R save(@Valid @RequestBody SwStCycle swStCycle) {
        PigxUser user = SecurityUtils.getUser();
        swStCycle.setCreatorId( user.getId());
        swStCycle.setCreatorName(user.getUsername());
        return R.ok(swStCycleService.save(swStCycle));
    }

    @SysLog("删除站间调票信息")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id) {
        return R.ok(swStCycleService.removeById(id));
    }

    @PutMapping
    @SysLog("修改站间调票信息")
    public R updateById(@Valid @RequestBody SwStCycle swStCycle) {
        swStCycle.setCheckStatus(SubwayStConstants.AUDIT_STATUS_WAIT);
        return R.ok(swStCycleService.updateById(swStCycle));
    }

    @PostMapping(value = "/import")
    @SysLog("导入站间调票信息")
    public R importExcel(@RequestParam("file") MultipartFile multipartFile) {
        try {
            File file = MultipartFileToFile.multipartFileToFile(multipartFile);
            Workbook workbook = ExcelUtil.chooseWorkbook(file);
            Sheet sheet = workbook.getSheetAt(0);
            IExcelParser excelParser = SpringContextHolder.getBean("commonExcelParse");
            List<SwStCycle> list = excelParser.parse(sheet, new SwStCycle());
            swStCycleService.saveBatch(list);
        } catch (Exception e) {
            return R.failed(e.getMessage(), "导入失败");
        }
        return R.ok("1", "导入成功");
    }

    @GetMapping("/export")
    public R exportExcel(SwStCycle swStCycle) throws IOException {
        List<SwStCycle> swStCycleList = swStCycleService.list(Wrappers.query(swStCycle));
        IExcelExport excelExport= SpringContextHolder.getBean("commonExcelExport");
        Workbook workbook = excelExport.parse(swStCycleList);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        workbook.write(baos);
        return R.ok(baos.toByteArray());
    }

    @PutMapping("/audit")
    @SysLog("审批站间调票信息")
    public R audit(@Valid @RequestBody SwStCycle swStCycle) {
        PigxUser user = SecurityUtils.getUser();
        swStCycle.setCheckPersonId(user.getUsername());
        swStCycle.setCheckTime(LocalDateTime.now());
        return R.ok(swStCycleService.updateById(swStCycle));
    }

}
